package com.carrie.mdg.myviewtext.activity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.carrie.mdg.myviewtext.R;
import com.carrie.mdg.myviewtext.db.DataBaseHelper;

import androidLib.activity.BaseActivity;

public class SQLiteTestActivity extends BaseActivity {
    private DataBaseHelper dbHelper;
    private Button createDatabase;
    private Button addData;
    private Button updateData;
    private Button deleteData;
    private Button queryData;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, SQLiteTestActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_sqlite_test);
        dbHelper = new DataBaseHelper(this, "BookStore.db", null, 2);
        createDatabase = (Button) findViewById(R.id.button_create_database);
        addData = (Button) findViewById(R.id.button_add_data);
        updateData = (Button) findViewById(R.id.button_update_data);
        deleteData = (Button) findViewById(R.id.button_delete_data);
        queryData = (Button) findViewById(R.id.button_query_data);
    }

    @Override
    protected void initEvent() {
        createDatabase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
            }
        });
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("name", "The Da Vinci Code");
                values.put("author", "Dan Brown");
                values.put("pages", 454);
                values.put("price", 16.96);
                db.insert("Book", null, values);
                values.clear();
                values.put("name", "The Lost Symbol");
                values.put("author", "Dan Brown");
                values.put("pages", 510);
                values.put("price", 19.95);
                db.insert("Book", null, values);
            }
        });
        updateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("price", 10.99);
                db.update("Book", values, "name = ?", new String[]{"The Da Vinci Code"});
            }
        });
        deleteData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.delete("Book", "pages > ?", new String[]{"500"});
            }
        });
        queryData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor result = db.query("Book", null, null, null, null, null, null);
                if (result.moveToFirst()) {
                    do {
                        String name = result.getString(result.getColumnIndex("name"));
                        String author = result.getString(result.getColumnIndex("author"));
                        int pages = result.getInt(result.getColumnIndex("pages"));
                        double price = result.getDouble(result.getColumnIndex("price"));
                        Toast.makeText(SQLiteTestActivity.this, name + "," + author + "," + pages + "," + price, Toast.LENGTH_SHORT).show();
                    } while (result.moveToNext());
                }
                result.close();
            }
        });
    }

    @Override
    protected void loadData() {

    }
}
